package beans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import util.ConnectionManager;
import util.Utilities;

/**
 *
 * @author Francis
 */
public class MemberBean {
    private String id;
    private String firstname;
    private String lastname;
    private String age ;
    private String gender;
    private String interest;
    private String country;
    private String zipcode;
    private String address;
    private String password;
    private String email; 
    
    /** Creates a new instance of MemberBean */
    private MemberBean(String id, String firstname, String lastname, String age, String gender,
            String interest, String country, String zipcode, String address, String password, String email) {
        this.id = id;
        this.firstname = firstname;
        this.lastname = lastname;
        this.age = age;
        this.gender = gender;
        this.interest = interest;
        this.country = country;
        this.zipcode = zipcode;
        this.address = address;
        this.password = password;
        this.email = email;
    }
    
    public MemberBean() {
    }
    public static boolean memberRegister(String id, String firstname, String lastname, String age, String gender,
            String interest, String country, String zipcode, String address, String password, String email) {
        Connection con = null;
        boolean success = false;
        try {
            con = ConnectionManager.connect();
            PreparedStatement update = con.prepareStatement("INSERT INTO member values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            //(userID, firstname, lastname, age, gender, interest, address, country, zipcode, password, identity, e-mail)
            update.setString(1, id);
            update.setString(2, firstname);
            update.setString(3, lastname);
            update.setString(4, age);
            update.setString(5, gender);
            update.setString(6, interest);
            update.setString(7, address);
            update.setString(8, country);
            update.setString(9, zipcode);
            update.setString(10, password);
            update.setString(11, "customer");
            update.setString(12, email);
            update.executeUpdate();
            success = true;
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Utilities.close(con);
        }
        return success;
    }
    public static MemberBean getMemberInfo(final String userID) {
        return oneMember("select * from member where userID='"+userID+"'");
    }
    
    static MemberBean oneMember(String sql) {
        ResultSet rs = null;
        Statement stmt = null;
        Connection con = null;
        MemberBean member = null;
        try {
            con = ConnectionManager.connect();
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            while(rs.next()) {
                member = oneSetOfResultSetToMember(rs);
                
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            Utilities.close(rs, stmt, con);
        }
        return member;
    }
    
    static MemberBean[] member(String sql) {
        ResultSet rs = null;
        Statement stmt = null;
        Connection con = null;
        MemberBean[] finalResult = new MemberBean[0];
        try {
            con = ConnectionManager.connect();
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            List<MemberBean> result = new ArrayList<MemberBean>();
            while(rs.next()) {
                MemberBean member = oneSetOfResultSetToMember(rs);
                result.add(member);
            }
            finalResult = (MemberBean[])result.toArray(finalResult);
        }catch (Exception e) {
            
        }finally {
            Utilities.close(rs, stmt, con);
        }
        return finalResult;
    }
    
    public static MemberBean oneSetOfResultSetToMember(final ResultSet rs) throws SQLException {
        
        String userID = rs.getString("userID");
        String firstname = rs.getString("firstname");
        String lastname = rs.getString("lastname");
        String age = rs.getString("age");
        String gender = rs.getString("gender");
        String interest = rs.getString("interest");
        String address = rs.getString("address");
        String country = rs.getString("country");
        String zipcode = rs.getString("zipcode");
        String password = rs.getString("password");
        String identity = rs.getString("identity");
        String email = rs.getString("email");
        MemberBean member = new MemberBean(userID, firstname, lastname, age, gender, interest, country, zipcode, address, password, email);
        return member;
    }
    
    public static boolean updateMemberInfo(String id, String firstname, String lastname, String age, String gender,
            String interest, String country, String zipcode, String address, String email, String password) {
        Connection con = null;
        try {
            con = ConnectionManager.connect();
            PreparedStatement update = con.prepareStatement("UPDATE member SET firstname=?, lastname=?, age=?, gender=?, interest=?, address=?, country=?, zipcode=?, email=?, password=? WHERE userID='"+id+"'");
            update.setString(1, firstname);
            update.setString(2, lastname);
            update.setString(3, age);
            update.setString(4, gender);
            update.setString(5, interest);
            update.setString(6, address);
            update.setString(7, country);
            update.setString(8, zipcode);
            update.setString(9, email);
            update.setString(10, password);
            update.executeUpdate();
            con.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Utilities.close(con);
        }
        return false;
    }
    public static boolean updateMemberInfo(String id, String age, String address, String email) {
        Connection con = null;
        try {
            con = ConnectionManager.connect();
            PreparedStatement update = con.prepareStatement("UPDATE member SET age=?, address=?,email=? WHERE userID='"+id+"'"); 
            update.setString(1, age);
            update.setString(2, address);
            update.setString(3, email);
            update.executeUpdate();
            con.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Utilities.close(con);
        }
        return false;
    }
    public String getId() {
        return id;
    }
    
    public String getFirstname() {
        return firstname;
    }
    
    public String getLastname() {
        return lastname;
    }
    
    public String getAge() {
        return age;
    }
    
    public String getGender() {
        return gender;
    }
    
    public String getInterest() {
        return interest;
    }
    
    public String getCountry() {
        return country;
    }
    
    public String getZipcode() {
        return zipcode;
    }
    
    public String getAddress() {
        return address;
    }
    
    public String getPassword() {
        return password;
    }
    
    public String getEmail() {
        return email;
    }
    
    public static boolean updatePassword(String username, String oldpassword, String password, String repassword) {
        ResultSet rs = null;
        Statement stmt = null;
        Connection con = null;
        try {
            con = ConnectionManager.connect();
            stmt = con.createStatement();
            rs = stmt.executeQuery("select password from member where userID ='"+username+"'");
            if (rs.next()) {
                String origin = rs.getString("password");
                if (origin.equals(oldpassword)) {
                    if (password.equals(repassword)) {
                        PreparedStatement update = con.prepareStatement("UPDATE member SET password=? WHERE userID='"+username+"'");
                        update.setString(1, password);
                        update.execute();
                        con.commit();
                        return true;
                    }
                }
            }
            return false;
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            Utilities.close(rs, stmt, con);
        }
        return false;
    }
}
